package com.powernode.controller;

import cn.hutool.core.lang.tree.Tree;
import com.powernode.dto.Result;
import com.powernode.entity.Category;
import com.powernode.service.CategoryService;
import com.powernode.validation.SaveGroup;
import com.powernode.validation.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/pms/admin/categories")
public class AdminCategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
    * 获取商品分类的树形列表
    */
    @GetMapping("/tree")
    public Result<List<Tree<Long>>> tree() {
        return Result.success(categoryService.tree());
    }

    /**
    * 根据商品分类id获取商品分类对象
    */
    @GetMapping("/{categoryId}")
    public Result getCategoryById(@PathVariable Long categoryId) {
        return Result.success(categoryService.getCategoryById(categoryId));
    }

    /**
    * 新增商品分类对象
     * @Validated(SaveGroup.class) 只校验新增校验组的数据和没有指定校验组（默认是Default组）的数据
    */
    @PostMapping
    public Result<Long> saveCategory(@RequestBody @Validated(SaveGroup.class) Category category){
        categoryService.saveCategory(category);
        return Result.success("新增商品分类成功",category.getId());
    }

    /**
    * 编辑商品分类对象
     * @Validated(UpdateGroup.class) 只校验编辑校验组的数据和没有指定校验组（默认是Default组）的数据
    */
    @PutMapping
    public Result editCategory(@RequestBody @Validated(UpdateGroup.class) Category category){
        categoryService.updateCategoryById(category);
        return Result.success("商品分类修改成功",null);
    }

    /**
    * 根据id删除商品分类对象
    */
    @DeleteMapping("/{categoryId}")
    public Result removeCategoryById(@PathVariable Long categoryId){
        categoryService.removeCategoryById(categoryId);
        return Result.success("商品分类删除成功",null);
    }
}
